package com.enjoyiot.module.system.service.sp;

import com.enjoyiot.framework.common.pojo.PageResult;
import com.enjoyiot.framework.common.util.object.BeanUtils;
import com.enjoyiot.module.system.controller.admin.sp.vo.SpCreateReqVO;
import com.enjoyiot.module.system.controller.admin.sp.vo.SpPageReqVO;
import com.enjoyiot.module.system.controller.admin.sp.vo.SpUpdateReqVO;
import com.enjoyiot.module.system.dal.dataobject.sp.SpDO;
import com.enjoyiot.module.system.dal.mysql.sp.SpMapper;
import com.enjoyiot.module.system.service.tenant.TenantService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.enjoyiot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.enjoyiot.module.system.enums.ErrorCodeConstants.SP_NOT_EXISTS;

/**
 * 视频地址 Service 实现类
 */
@Service
@Validated
public class SpServiceImpl implements SpService {

    @Resource
    private SpMapper spMapper;

    @Resource
    private TenantService tenantService;

    @Override
    public Long createSp(SpCreateReqVO createReqVO) {
        // 插入
        SpDO sp = BeanUtils.toBean(createReqVO, SpDO.class);
        spMapper.insert(sp);
        // 返回
        return sp.getId();
    }

    @Override
    public void updateSp(SpUpdateReqVO updateReqVO) {
        // 校验存在
        validateSpExists(updateReqVO.getId());
        // 更新
        SpDO updateObj = BeanUtils.toBean(updateReqVO, SpDO.class);
        spMapper.updateById(updateObj);
    }

    @Override
    public void deleteSp(Long id) {
        // 校验存在
        validateSpExists(id);
        // 删除
        spMapper.deleteById(id);
    }

    private void validateSpExists(Long id) {
        if (spMapper.selectById(id) == null) {
            throw exception(SP_NOT_EXISTS);
        }
    }

    @Override
    public SpDO getSp(Long id) {
        return spMapper.selectById(id);
    }

    @Override
    public List<SpDO> getSpList(Collection<Long> ids) {
        return spMapper.selectBatchIds(ids);
    }

    @Override
    public List<SpDO> getSpList() {
        return spMapper.selectAllSpAddresses();
    }

    @Override
    public PageResult<SpDO> getSpPage(SpPageReqVO pageReqVO) {

        return spMapper.selectPage(pageReqVO);
    }
    @Override
    public SpDO getSpBySpNumber(String spNumber) {
        return spMapper.selectBySpNumber(spNumber);
    }

    @Override
    public List<SpDO> getSpListByCgqNumber(Integer cgqNumber) {
        return spMapper.selectListByCgqNumber(cgqNumber);
    }
}